import {callApi} from '../../util/callApi';


/**
 * 获取文章列表
 * @returns {function()}
 */
const getList = (formdata) => {
  return (dispatch) => {
    const request_url = '/admin/post/list';
    return callApi.get(request_url,formdata).then(res=>{
      if(res.code === 0){
        return Promise.resolve(dispatch({
          type: 'post_list',
          posttype : formdata.type,
          pagination : res.data.pagination,
          postlist : res.data.results
        }))
      }else{
        return Promise.reject(res)
      }
    });
  }
}

/**
 * 获取文章详情
 * @returns {function()}
 */
const getDetail = (formdata) => {
  return (dispatch) => {
    const request_url = '/admin/post/detail';
    if(formdata._id){
      return callApi.get(request_url,formdata).then(res=>{
        if(res.code === 0){
          return Promise.resolve(dispatch({
            type: 'post_detail',
            detail : res.data
          }))
        }else{
          return Promise.reject(res)
        }
      });
    }else{
      return Promise.resolve(dispatch({
        type: 'post_detail',
        detail : {}
      }))
    }
  }
}

/**
 * 添加分类
 * @param formdata
 * @returns {function()}
 */
const postAddPost = (formdata) => {
  return (dispatch) => {
    const request_url = '/admin/post/add';
    return callApi.post(request_url,JSON.stringify(formdata)).then(res=>{
      if(res.code === 0){
        return Promise.resolve(dispatch({
          type : 'post_add',
          info : res.data
        }));
      }else{
        return Promise.reject(res)
      }
    });
  }
}

/**
 * 编辑分类
 * @param formdata
 * @returns {function()}
 */
const postEditPost = (formdata) => {
  return (dispatch) => {
    const request_url = '/admin/post/update';
    return callApi.post(request_url,JSON.stringify(formdata)).then(res=>{
      if(res.code === 0){
        return Promise.resolve(dispatch({
          type : 'post_edit',
          index : formdata.index,
          info : res.data
        }));
      }else{
        return Promise.reject(res)
      }
    });
  }
}

/**
 * 删除文章到回收站
 * @param formdata
 * @returns {function()}
 */
const postTrashPost = (formdata) => {
  return (dispatch) => {
    const request_url = '/admin/post/trash';
    return callApi.post(request_url,JSON.stringify(formdata)).then(res=>{
      return Promise.resolve(dispatch({
        type : 'post_delete',
        index : formdata.index
      }));
    });

  }
}

/**
 * 彻底删除文章
 * @param formdata
 * @returns {function()}
 */
const postDeletePost = (formdata) => {
  return (dispatch) => {
    const request_url = '/admin/post/delete';
    return callApi.post(request_url,JSON.stringify(formdata)).then(res=>{
      return Promise.resolve(dispatch({
        type : 'post_delete',
        index : formdata.index
      }));
    });

  }
}

/**
 * 恢复
 * @param formdata
 * @returns {function()}
 */
const postRecoverPost = (formdata) => {
  return (dispatch) => {
    const request_url = '/admin/post/recover';
    return callApi.post(request_url,JSON.stringify(formdata)).then(res=>{
      return Promise.resolve(dispatch({
        type : 'post_delete',
        index : formdata.index
      }));
    });

  }
}

export default {
  getList,
  getDetail,
  postAddPost,
  postEditPost,
  postTrashPost,
  postRecoverPost,
  postDeletePost
}